(function() {

    var
    ns = {
        validation : project.namespace('validation')
    },
    vr = ns.validation.validationResult,
    ve = ns.validation.validationEvent,
    $ = juliet.jQuery;

    var doSomethingTimer;

    function doSomething(value, o) {
        doSomethingTimer = setTimeout(function() {
            if (value == 'trueLogin') o.continueValidation(vr.SUCCESS);
            else o.continueValidation(vr.FAILED);
        }, 10000);
    };

    function stopDoSomething() {
        clearTimeout(doSomethingTimer);
    }

    function initialize() {
        var
        validationManager = new ns.validation.ValidationManager(),
        loginCustomValidatorErrorText = 'Field must contains only latin chars and numbers';

        validationManager.initialize([
            // account
            { fieldSelector : '#login', validators : [
                { name : 'Regex', options : {
                    template : 'latin-and-number',
                    validationEvents : ve.KEYUP | ve.CHANGE,
                    forceError : true, errorText : loginCustomValidatorErrorText
                } },
                { name : 'NotEmpty' },
                { name : 'Length', options : { min : 5 } },
                { name : 'Custom', options : { validateCallback : doSomething, cancelCallback : stopDoSomething } }
            ] },
            { fieldSelector : '#password', validators : [
                { name : 'NotEmpty' },
                { name : 'Verify', options : { group : 'password' } }
            ] },
            { fieldSelector : '#passwordConfirm', validators : [
                { name : 'NotEmpty' },
                { name : 'Verify', options : { group : 'password', suppressError : true } }
            ] },
            { fieldSelector : '#email', validators : [
                { name : 'Regex', options : { template : 'email' } }
            ] },

            // personal information
            { uid : 'last-name', fieldSelector : '#lastName', validators : [
                { name : 'NotEmpty' }
            ] },


            { fieldSelector : '#country', validators : [
                { name : 'Select', options : { wrongValues : ['usa'] } }
            ] },
            { fieldSelector : '#agreement', validators : [
                { name : 'CheckBox', options : { wrongValue : false } }
            ] },
            { fieldSelector : 'input[name=sex]', validators : [
                { name : 'Radio', options : { correctValue : 'female' } }
            ] }
        ], '.submit.information');

        validationManager.setChainActivity('last-name', false);

        $('#submit').click(function() {
            return (validationManager.validate() == vr.SUCCESS);
        });
    };

    $(document).ready(function() {
        initialize();
    });

})();
